package com.zhny.project.txt.calculation.service.impl;

import com.zhny.common.constant.UserConstants;
import com.zhny.common.utils.DateUtils;
import com.zhny.common.utils.StringUtils;
import com.zhny.common.utils.security.ShiroUtils;
import com.zhny.common.utils.text.Convert;
import com.zhny.project.txt.calculation.domain.TxtCalculation;
import com.zhny.project.txt.calculation.mapper.TxtCalculationMapper;
import com.zhny.project.txt.calculation.service.ITxtCalculationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * txt分析计算记录Service业务层处理
 *
 * @author yanqun
 * @date 2020-01-18
 */
@Service
public class TxtCalculationServiceImpl implements ITxtCalculationService
{
    @Autowired
    private TxtCalculationMapper txtCalculationMapper;

    /**
     * 查询txt分析计算记录
     *
     * @param calculationKey txt分析计算记录ID
     * @return txt分析计算记录
     */
    @Override
    public TxtCalculation selectTxtCalculationById(String calculationKey)
    {
        return txtCalculationMapper.selectTxtCalculationById(calculationKey);
    }

    /**
     * 查询txt分析计算记录列表
     *
     * @param txtCalculation txt分析计算记录
     * @return txt分析计算记录
     */
    @Override
    public List<TxtCalculation> selectTxtCalculationList(TxtCalculation txtCalculation)
    {
        return txtCalculationMapper.selectTxtCalculationList(txtCalculation);
    }

    /**
     * 新增txt分析计算记录
     *
     * @param txtCalculation txt分析计算记录
     * @return 结果
     */
    @Override
    public int insertTxtCalculation(TxtCalculation txtCalculation)
    {
        txtCalculation.setDeleteFlag(0);
        txtCalculation.setUpdateBy(ShiroUtils.getUserId().toString());
        txtCalculation.setUpdateTime(DateUtils.getNowDate());
        txtCalculation.setCreateBy(ShiroUtils.getUserId().toString());
        txtCalculation.setCreateTime(DateUtils.getNowDate());
        txtCalculation.setCreateTime(DateUtils.getNowDate());
        return txtCalculationMapper.insertTxtCalculation(txtCalculation);
    }

    /**
     * 修改txt分析计算记录
     *
     * @param txtCalculation txt分析计算记录
     * @return 结果
     */
    @Override
    public int updateTxtCalculation(TxtCalculation txtCalculation)
    {
        txtCalculation.setUpdateTime(DateUtils.getNowDate());
        return txtCalculationMapper.updateTxtCalculation(txtCalculation);
    }

    /**
     * 删除txt分析计算记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTxtCalculationByIds(String ids)
    {
        return txtCalculationMapper.deleteTxtCalculationByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除txt分析计算记录信息
     *
     * @param calculationKey txt分析计算记录ID
     * @return 结果
     */
    public int deleteTxtCalculationById(String calculationKey)
    {
        return txtCalculationMapper.deleteTxtCalculationById(calculationKey);
    }

    /**
     * 校验分析计算名称
     */
    public String checkCalculationNamehUnique(TxtCalculation txtCalculation){
        String calculationKey = txtCalculation.getCalculationKey();
        TxtCalculation info = txtCalculationMapper.checkCalculationNamehUnique(txtCalculation.getCalculationName());
        if (StringUtils.isNotNull(info) && !info.getCalculationKey() .equals(calculationKey)) {
            return UserConstants.MESSAGE_NOT_UNIQUE;
        }
        return UserConstants.MESSAGE_UNIQUE;
    }

}
